package core;

import Utils.tempWorkTree;
import fileoperation.FileCreation;
import fileoperation.FileDeletion;
import gitobject.Commit;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class JitBranch {

//新建分支
    public static void newBranch(String name) throws IOException {
        tempWorkTree.updateWorkTree();


        File file=new File(tempWorkTree.lastCommitLoc); 
        File[] files = file.listFiles();
        for (File f : files) {
            if(f.getName().equals(name)){
                System.out.println("branch "+name+" is existed");
                return;
            }
        }
        String branchName = JitBranch.getBranchName(tempWorkTree.gitDir);
        String lastcommit=Commit.getLastCommit();

        FileCreation.createFile(tempWorkTree.commitHistoryLoc,name,"0000000000000000000000000000000000000000 "+lastcommit+" branch:created from "+branchName+"\n");

        FileCreation.createFile(tempWorkTree.lastCommitLoc,name,lastcommit);
        System.out.println("successfully created branch "+name+"\n");
    }
    //删除分支
    public static void deleteBranch(String name) throws IOException {
        tempWorkTree.updateWorkTree();

        File file=new File(tempWorkTree.commitHistoryLoc); 
        File[] files = file.listFiles();
        for (File f : files) {
            if(f.getName().equals(name)){
            	f.delete();
            	System.out.println("delete branch "+name+" complete!");
            	String lastcommit=Commit.getLastCommit();
           
            	return;
            }
        }
        System.out.println("branch "+name+" doesnt exist!");
    }
    
    //查看分支
    public static void showBranch() throws IOException {
    	tempWorkTree.updateWorkTree();
    	File file=new File(tempWorkTree.lastCommitLoc);
        File[] files = file.listFiles();
        String currentBranchName = JitBranch.getBranchName(tempWorkTree.gitDir);
        
        for (File f : files) {
        	if(f.getName().equals(currentBranchName))
        		System.out.println("\033[32m" + "*"+ f.getName() );
        	else
        		System.out.println("\033[30m"+f.getName() );
        }
    }

    public static void checkout(String name) throws IOException {

        tempWorkTree.updateWorkTree();
        boolean tag=false;
        File file=new File(tempWorkTree.lastCommitLoc);
        File[] files = file.listFiles();
        for (File f : files) {
            if(f.getName().equals(name)){
                tag=true;
            }
        }
        if(tag==false){
            return;
        }
        String oldname=JitBranch.getBranchName(tempWorkTree.gitDir);
        if(oldname.equals(name)){
            System.out.println("已切换");
            return;
        }
        String oldlastcommit=Commit.getLastCommit();

        FileOutputStream fileOutputStream = new FileOutputStream(tempWorkTree.gitDir + File.separator + "HEAD",false);
        String temp="ref: refs"+File.separator+"heads"+File.separator+name;
        fileOutputStream.write(temp.getBytes(StandardCharsets.UTF_8));
        fileOutputStream.close();
        //lastcommit checkout:moving from to
        FileOutputStream headWrite = new FileOutputStream(tempWorkTree.logHead, true);
        temp=oldlastcommit+" "+Commit.getLastCommit()+" checkout:moving from "+oldname+" to "+name+"\n";
        headWrite.write(temp.getBytes(StandardCharsets.UTF_8));
        headWrite.close();
        String commitKey=Commit.getLastCommit();
        JitReset.resetHard(commitKey,"1");
    }


    public static String getBranchName(String path) throws IOException {

        File HEAD = new File(path + File.separator + "HEAD");
        String branchName = Commit.getValue(HEAD).substring(16).replace("\n", "");
        return branchName;
    }
}
